'use strict';

const Controller = require('egg').Controller;
const moment = require('moment')

class News extends Controller {
  async classifyList() {
    // 新闻分类列表
    let {
      ctx
    } = this
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    let classify = await ctx.model.Classify.findAndCountAll({
      order: [
        ['cy_id', 'DESC']
      ],
      limit: pageshow,
      offset: (page - 1) * pageshow
    })

    ctx.body = {
      code: 200,
      data: {
        list: classify.rows.map(item => {
          let obj = JSON.parse(JSON.stringify(item))
          return {
            ...obj,
            created_at: moment(obj.created_at * 1000).format('YYYY-MM-DD HH:mm:ss')
          }
        }),
        count: classify.count
      },
      msg: '操作成功'
    }
  }
  async handleClassify() {
    let {
      ctx
    } = this
    // 操作新闻分类
    let action = ctx.request.body.action
    let cy_name = ctx.request.body.cy_name
    let cy_id = ctx.request.body.cy_id
    if (action == 'add') {
      await ctx.model.Classify.create({
        cy_name,
        cy_num: 0,
        created_at: Date.parse(new Date()) / 1000
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    }
    if (cy_id && cy_name) {
      await ctx.model.Classify.update({
        cy_name,
      }, {
        where: {
          cy_id
        }
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    }
    if (action == 'del') {
      await ctx.model.Classify.destroy({
        where: {
          cy_id
        }
      })

      await ctx.model.News.destroy({
        where: {
          cy_id
        }
      })

      return ctx.body = {
        code: 200,
        data: {},
        msg: '删除成功'
      }
    }
  }
  async newsList() {
    // 新闻列表
    let {
      ctx
    } = this
    let news_id = ctx.request.body.news_id
    let cy_id = ctx.request.body.cy_id
    let page = ctx.request.body.page
    let pageshow = ctx.request.body.pageshow
    if (news_id && cy_id) {
      // 删除文章
      let classify = await ctx.model.Classify.findOne({
        where: {
          cy_id
        }
      })
      await classify.set('cy_num', classify.cy_num - 1).save()

      await ctx.model.News.destroy({
        where: {
          news_id
        }
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    } else {

      let news = await ctx.model.News.findAndCountAll({
        order: [
          ['created_at', 'DESC']
        ],
        include: {
          model: ctx.model.Classify,
          attributes: ['cy_name'],
        },
        limit: pageshow,
        offset: (page - 1) * pageshow
      })

      return ctx.body = {
        code: 200,
        data: {
          list: news.rows.map(item => {
            let obj = JSON.parse(JSON.stringify(item))
            return {
              ...obj,
              created_at: moment(obj.created_at * 1000).format('YYYY-MM-DD HH:mm:ss'),
            }
          }),
          count: news.count
        },
        msg: '操作成功'
      }
    }
  }
  async handleNews() {
    // 操作新闻文章
    let {
      ctx
    } = this
    let news_id = ctx.request.body.news_id
    let news_title = ctx.request.body.news_title
    let news_cover = ctx.request.body.news_cover
    let cy_id = ctx.request.body.cy_id
    let introduce = ctx.request.body.introduce
    let article = ctx.request.body.article


    if (news_id) {
      // 更新新闻文章
      let news = await ctx.model.News.findOne({
        where: {
          news_id
        }
      })
      let oldclassify = await ctx.model.Classify.findOne({
        where: {
          cy_id: news.get('cy_id')
        }
      })
      let newclassify = await ctx.model.Classify.findOne({
        where: {
          cy_id
        }
      })
      if (cy_id == news.get('cy_id')) {

      } else {
        await oldclassify.set('cy_num', oldclassify.cy_num - 1).save()
        await newclassify.set('cy_num', newclassify.cy_num + 1).save()
      }

      await ctx.model.News.update({
        news_title,
        news_cover,
        cy_id,
        introduce,
        article
      }, {
        where: {
          news_id
        }
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    } else {
      //　添加新闻文章
      let classify = await ctx.model.Classify.findOne({
        where: {
          cy_id
        }
      })
      await classify.set('cy_num', classify.cy_num + 1).save()

      await ctx.model.News.create({
        news_title,
        news_cover,
        cy_id,
        introduce,
        article,
        created_at: Date.parse(new Date()) / 1000
      })
      return ctx.body = {
        code: 200,
        data: {},
        msg: '操作成功'
      }
    }
  }
}

module.exports = News;